import axios from 'axios'
import router from '@/router'

export function request(configUrl) {
    const instance = axios.create({
        // baseURL: "https://m.maizuo.com",
        timeout: 5000,
        headers: {
            "X-Client-Info": ' {"a":"3000","ch":"1002","v":"5.0.4","e":"1614444947506870565437441","bc":"310100"}',
        }
    })

    // 请求拦截
    instance.interceptors.request.use(config => {
        return config;
    }, err => {
        return Promise.reject(err)
    })

    // 响应拦截
    instance.interceptors.response.use(res => {
        return res;
    }, err => {
        return Promise.reject(err)
    })

    return instance(configUrl)
}


export function request2(configUrl) {
    const instance = axios.create({
        baseURL: "http://112.74.99.5:3000/web",
        timeout: 5000,
    })

    // 请求拦截
    instance.interceptors.request.use(config => {
        // 添加请求头token
        let token = localStorage.getItem('token')
        let id = localStorage.getItem('id')
        if (token && id) {
            config.headers.Authorization = "Bearer " + token
        }
        return config;
    }, err => {
        return Promise.reject(err)
    })

    // 响应拦截
    instance.interceptors.response.use(res => {
        return res;
    }, err => {
        console.dir(err);
        // 接收到服务器返回登录状态失效,需要重新登录的时候，跳转到登录页。
        if (err.response.status == 401 || err.response.status == 500) {
            localStorage.removeItem('token');
            router.push('/login')
        }
        return Promise.reject(err)

    })

    return instance(configUrl)
}
